import os
import datetime
from django.conf import settings
from utils.common import renameuploadimg


# 处理上传的文件
def ImageUpload(request, dirs):
    """
    request:请求
    dirs:要上传到那个目录
    """
    image = request.FILES['file']
    print(image)
    msg = {}
    if not image:
        msg['code'] = 400
        msg['msg'] = "上传的图片不能为空"
        return msg
    notimg_file = []
    img_file = []
    try:
        img_name = image.name
        # 图片类型content-type检查
        if not image.content_type.startswith('image/') and not image.content_type.startswith(
                'video/') and not image.content_type.startswith('audio/'):
            msg['code'] = 400
            msg['msg'] = "请上传正确的文件格式"
            return msg

        if not img_name.endswith(
                ('.jpg', '.jpeg', '.png', 'gif', '.bmp', '.JPG', '.JPEG', '.PNG', 'GIF', '.BMP', '.mp4', '.flv')):
            notimg_file.append(img_name)

        if image.size > 1024 * 500000:
            msg['code'] = 400
            msg['msg'] = "图片大小不能超过500M"
            return msg
        else:
            curr_time = datetime.datetime.now()
            image_name = renameuploadimg(img_name)
            img_task_dir = dirs  # 对应models中的上传路径
            sub_path = os.path.join(settings.MEDIA_ROOT, img_task_dir)
            if not os.path.exists(sub_path):
                os.makedirs(sub_path)
            image_path = os.path.join(sub_path, image_name)
            # web_img_url = settings.MEDIA_URL + img_task_dir + "/" + time_path + "/" + image_name#相对路径/media/xxx/xxxx/xxx.png
            web_img_url = "http://127.0.0.1:8000" + settings.MEDIA_URL + img_task_dir + "/" + image_name  # 绝对路径http://xxx.xxx.com/media/xxx/xxxx/xxx.png
            f = open(image_path, 'wb')
            for i in image.chunks():
                f.write(i)
            f.close()
            img_file.append(web_img_url)
    except Exception as e:
        msg['code'] = 400
        msg['msg'] = '上传失败'
        return msg
    if notimg_file:
        msg['code'] = 400
        msg['msg'] = '请检查是否支持的文件，失败文件部分如下：{0}'.format(','.join(notimg_file[:10]))
        return msg
    msg['code'] = 2000
    msg['img'] = img_file  # ['/media/xxx/xxx/xxx.png']
    msg['msg'] = '上传成功'
    return msg
